package com.sc.auction.sms.Controller;

import com.sc.auction.common.util.R;
import com.sc.auction.sms.Service.SmsService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("auction")
@CrossOrigin
public class SmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/sms")
    @ApiOperation("短信发送模块")
    public R sendMsg(String mobile){
        try {
            //生成验证码
            String random = RandomStringUtils.random(4, "0123456789");
            //发送验证码
            smsService.send(mobile,random);
            //将验证码存入redis
            redisTemplate.opsForValue().set(mobile,random,30, TimeUnit.MINUTES);
            return R.ok().message("短信发送成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("短信发送失败");
        }
    }
}
